/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-07-08 16:28:06
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-08-05 22:49:30
 */
import { Injectable } from '@nestjs/common';
import { InjectRepository ,} from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from '../entity/user.entity';
const bcrypt = require('bcrypt');
const saltOrRounds = 10;
@Injectable()
export class LoginService {
    public constructor(@InjectRepository(User) private readonly user: Repository<User>) { }

    public async registerUser({ username, password }) {
        // console.log('username:',username)
        // console.log('password:',password)
        const changedPwd = password;
        const user = await this.user.findOne({ where: { username } });
        console.log('user-user:',user)
        if (!user) {

            try {
                console.log('pwd', changedPwd)
                console.log('saltOrRounds',saltOrRounds)
                const pwd = await bcrypt.hash(changedPwd, saltOrRounds);

            await this.user
                .createQueryBuilder()
                .insert()
                .into(User)
                .values([
                    {
                        username,
                        password: pwd
                    },
                ])
                .execute();

            return {
                code: 0,
                message: 'ok',
            };
            } catch (error) {
                console.log(error);
                 return {
            code:500,
            message: '代码错误',
        };
            }

        }

        return {
            code: 1,
            message: '用户已存在',
        };
    }

    public async login({ username, password }) {
        const salt = bcrypt.genSaltSync(10);
        const changedPwd = password;
        const hash = bcrypt.hashSync(changedPwd, salt);
        const user = await this.user.findOne({ where: { username, password } });


        if (user) {
            const result = await bcrypt.compare(user.password, hash)
            if (result) {
                //登录成功
                return {
                    code: 200,
                    message: '登录成功',
                    user,
                };
            } else {
                //登录失败
                 return {
                    code:400,
                    message: '登录失败',
                };
            }
        }


    }

    async findOneUser({ username, password }) {
        const user = await this.user.findOne({ where: { username} });
        console.log(username)
         console.log(password)
        if (!user) {
            return {}
        } else {
             return {user}
        }
    }
    async registerUserWX({ username, phone }) {
        const user = await this.user.findOne({ where: { phone } });
        if (!user) {
            const changedPwd = '123456';
           const pwd = await bcrypt.hash(changedPwd, saltOrRounds);
           await this.user
                .createQueryBuilder()
                .insert()
                .into(User)
                .values([
                    {
                        username,
                        phone: phone,
                        password: pwd
                    },
                ])
                .execute();
        } else {
             return {user}
        }
    }
}
